package lib

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/m"
)

// 获取当前jackpot活动，如果有多个活动存在，只返回一个活动
func GetCurrentJackpotActivity() (jactiv data.JackpotActivity, activ data.ActActivity, err error) {
	nowUnix := utils.TNow().Unix()
	tplLst, ok := data.Filters("ActActivities", func(val interface{}) bool {
		valT := val.(data.ActActivity)
		actTyp := int64(valT.ActTyp)
		beginTime, _ := utils.LayoutToTime(valT.BeginTime)
		endTime, _ := utils.LayoutToTime(valT.EndTime)
		return actTyp == data.ActivityEnum.Jackpot && beginTime.Unix() <= nowUnix && nowUnix <= endTime.Unix()
	})

	if !ok || len(tplLst) == 0 {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到激活活动配置,ActActivities,或者没有活动")
		return
	}
	activ = tplLst[0].(data.ActActivity)

	tpl, ok := data.Get("JackpotActivitys", activ.ActivId)
	if !ok {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到激活活动配置,JackpotActivitys")
		return
	}
	jactiv = tpl.(data.JackpotActivity)
	return
}

// 获取当前用户的组头奖信息
func GetJackpotInfo(u *m.User) (r map[int64]int64) {
	group := m.GetJGroup(1)
	r = group.GetBonus()
	return
}

// 获取大奖类型
func GetJackpotType(tid int64) (typ int64, err error) {
	tpl, ok := data.Get("Goods", uint64(tid))
	if !ok {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到该配置表Goods")
		return
	}
	tplT := tpl.(data.Good)
	switch int64(tplT.Category) {
	case data.GoodsCategoryEnum.CategoryLv11:
		switch int64(tplT.Type) {
		case data.GoodsTypeEnum.JackpotGrand:
			typ = data.JackpotEnum.Grand
		case data.GoodsTypeEnum.JackpotMajor:
			typ = data.JackpotEnum.Major
		case data.GoodsTypeEnum.JackpotMini:
			typ = data.JackpotEnum.Mini
		}
	}
	return
}

// 通过类型获取Jackpot的tid
func GetJackpotTidByType(typ int64) (r int64) {
	tplLst, ok := data.Filters("Goods", func(val interface{}) bool {
		valT := val.(data.Good)
		return int64(valT.Category) == data.GoodsCategoryEnum.CategoryLv11
	})
	if !ok {
		return
	}
	for _, item := range tplLst {
		itemT := item.(data.Good)
		if int64(itemT.Type) == typ {
			return int64(itemT.Id)
		}
	}
	return
}

// 抽奖
func LuckDraw(u *m.User, spin int64, gmTyp int64) (r m.BaseKV, plate int64, err error) {
	ja := m.GetJActiv(1)
	jg := m.GetJGamer(u.Uid.Hex())

	tpl, ok := data.GetAllDatas("SlotJackpots")
	if !ok {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "没有找到SlotJackpots配置表")
		return
	}

	jTid := GetJackpotTidByType(gmTyp)
	group := m.GetJGroup(1)
	// 玩家没有中奖，且有大奖可领，进行匹配
	if jg.Time == 0 && group.GetBonusState() {
		weights := make([]data.WeightV, 0)
		for _, item := range tpl {
			itemT := item.(data.SlotJackpot) // itemT.id:轮盘id，itemT.Randv[].Key:老虎机倍率，itemT.Randv[].Value:权重
			for _, item1 := range itemT.Randv {
				if int64(item1.Key) == spin {
					weights = append(weights, data.WeightV{Value: itemT.Id, Weight: item1.Value}) // WeightV.Value:轮盘id，WeightV.Weight:权重
				}
			}
		}
		r, plate = group.LuckDraw(ja.Threshold, ja.GoodLst, weights, jTid)
	} else { // 没有大奖可领，进行匹配
		weights := make([]data.WeightV, 0)
		for _, item := range tpl {
			// itemT.id:轮盘id，itemT.Randv[].Key:老虎机倍率，itemT.Randv[].Value:权重
			itemT := item.(data.SlotJackpot)
			if _, ok := ja.GoodNormal[int64(itemT.Id)]; !ok { //过滤掉奖金
				continue
			}
			for _, item1 := range itemT.Randv {
				if int64(item1.Key) == spin { // 老虎机倍率
					// WeightV.Value:轮盘id，WeightV.Weight:权重
					weights = append(weights, data.WeightV{Value: itemT.Id, Weight: item1.Value})
				}
			}
		}
		r, plate = group.NormalLuckDraw(ja.Threshold, ja.GoodLst, weights)
	}
	r.Val *= spin
	return
}

// gm彩金池添加彩金
func GMJackpotAddBonus(u *m.User, typ int64, amount int64) {
	tplLst, ok := data.Filters("Goods", func(val interface{}) bool {
		valT := val.(data.Good)
		return int64(valT.Category) == data.GoodsCategoryEnum.CategoryLv11
	})
	if !ok {
		return
	}
	tid := int64(0)
	for _, item := range tplLst {
		itemT := item.(data.Good)
		if int64(itemT.Type) == typ {
			tid = int64(itemT.Id)
			break
		}
	}
	group := m.GetJGroup(1)
	group.GMAddBonus(tid, amount)
}

func GetJackpotX(median int64) (r int64) {
	si, err := m.GetShopItemT(3, 3001)
	if err != nil {
		return
	}
	for _, item := range si.Goods {
		if int64(item.Id) == median {
			return int64(item.Value)
		}
	}
	return
}

// 重置自己获奖
func JackpotReset(u *m.User) {
	jg := m.GetJGamer(u.Uid.Hex())

	jg.Time = 0
	jg.Update(jg)
}
